VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "ClsComisionista"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'Clase Comision
Public WithEvents orset As ClsRecordset
Attribute orset.VB_VarHelpID = -1

Public Event DataChanged()

'Miembros de la Clase
Public codigo As String
Public codTipoDocumento As String
Public nit As String
Public nombre As String

' Constructor y Destructor
Private Sub Class_Initialize()
    Set orset = New ClsRecordset
    orset.setCampos dbCampos(com)
    orset.setTablas Array(com)
End Sub
Private Sub Class_Terminate()
    Set orset = Nothing
End Sub
' Generic Data management methods
Public Sub consulta()
    orset.consulta
End Sub
Public Sub consultaDatos(codig As String)
    orset.consulta Array(com_id & "='" & codig & "'")
    orset.CierraRec
End Sub
Public Sub consultaTipo(codTipo As String)
    orset.consulta Array(com_tdo_id & "=" & codTipo)
End Sub
Public Sub consultaNitNombre(optNit As String, optNombre As String)
    orset.consulta Array(com_nit & " LIKE '" & optNit & "%'", _
                         com_nombre & " LIKE '" & optNombre & "%'")
End Sub
Public Sub consultaNitNombreTipo(optNit As String, optNombre As String, codTipo As String)
    orset.consulta Array(com_tdo_id & "=" & codTipo, _
                         com_nit & " LIKE '" & optNit & "%'", _
                         com_nombre & " LIKE '" & optNombre & "%'")
End Sub

Public Sub buscarGrid(tipoDocCodigo As String, Optional nit As String = "", Optional nombre As String = "")
    Dim campos As Variant
    Dim tablas As Variant
    Dim condiciones As Variant
    Dim orden As Variant
    
    Dim camposBuscar As String
    Dim condicionesBuscar As String
    camposBuscar = ""
    
    camposBuscar = camposBuscar + "CASE WHEN "
        If nit <> "" Then
            camposBuscar = camposBuscar + com_nit + " LIKE '" + nit + "%' AND "
        End If
        If nombre <> "" Then
            camposBuscar = camposBuscar + com_nombre + " LIKE '" + nombre + "%' AND "
        End If
        If nit = "" And nombre = "" Then
            camposBuscar = camposBuscar + com_nit + " LIKE '" + nit + "%' AND "
        End If
    camposBuscar = Strings.Left(camposBuscar, Strings.Len(camposBuscar) - 4) & " THEN 1 "
        If nit <> "" Then
            camposBuscar = camposBuscar + "WHEN " & com_nit & " LIKE '" + nit + "%' THEN 2 "
            condicionesBuscar = condicionesBuscar + com_nit + " LIKE '" + nit + "%' OR "
        End If
        If nombre <> "" Then
            camposBuscar = camposBuscar & "WHEN " & com_nombre & " LIKE '" + nombre + "%' THEN 3 "
            condicionesBuscar = condicionesBuscar & com_nombre & " LIKE '" + nombre + "%' OR "
        End If
        If nit = "" And nombre = "" Then
            camposBuscar = camposBuscar + "WHEN " & com_nit & " LIKE '" + nit + "%' THEN 2 "
            condicionesBuscar = condicionesBuscar + com_nit + " LIKE '" + nit + "%' OR "
        End If
    condicionesBuscar = Strings.Left(condicionesBuscar, Strings.Len(condicionesBuscar) - 3)
    camposBuscar = camposBuscar + "END AS orden"
    
    campos = concatenaVectores(dbCampos(com), Array(camposBuscar))
    tablas = Array(com)
    condiciones = Array(com_tdo_id & "=" & tipoDocCodigo, condicionesBuscar)
    orden = Array("orden", com_nombre)
    
    orset.consultaGral campos, tablas, condiciones, orden
End Sub

Private Sub Reload_members()
    With orset
        codigo = .getDato(com_id)
        codTipoDocumento = .getDato(com_tdo_id)
        nit = .getDato(com_nit)
        nombre = .getDato(com_nombre)
    End With
    RaiseEvent DataChanged
End Sub

Public Sub Nuevo(newTdoId As String, newNit As String, newNombre As String)
    orset.sInsert Array("", newTdoId, newNit, newNombre)
End Sub
Public Sub Modificar(codig As String, newTdoId As String, newNit As String, newNombre As String)
    orset.sUpdate Array(codig, newTdoId, newNit, newNombre)
End Sub
Public Sub Borrar(codig As String)
    orset.sDelete Array(com_id & "='" & codig & "'")
End Sub
Private Sub orset_DataChanged()
    Reload_members
End Sub
